<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="(item) in list"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hAdd">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
// 导入api函数
import { getRole } from '@/api/role'
import { getInfoById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadEmployeesList()
    this.loadRoleByIdDetail()
  },
  methods: {
    async hAdd() {
      try {
        const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
        console.log('assignRoles', res)
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    closeDialog() {
      this.$emit('close')
    },
    async loadEmployeesList() {
      try {
        const res = await getRole()
        console.log('getRole', res)
        this.list = res.data.rows
      } catch (error) {
        console.log(error)
      }
    },
    async loadRoleByIdDetail() {
      try {
        const res = await getInfoById(this.curId)
        console.log('getInfoById', res)
        this.roleIds = res.data.roleIds || []
      } catch (error) {
        console.log(error)
      }
    }
  }

}
</script>
